package com.yong.ssj.db.dao.sys;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.yong.ssj.db.dto.cost.CostMethodDto;
import com.yong.ssj.db.dto.customer.CustomerDto;
import com.yong.ssj.db.dto.sys.UserDto;


@Repository(value="userDaoImpl")
public class UserDaoImpl implements IUserDao{
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Override
	public UserDto query(String username) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_user where user_name=?";
		UserDto user = null;
		try {
			user = jdbcTemplate.queryForObject(sql, new Object[]{username},new BeanPropertyRowMapper<UserDto>(UserDto.class));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;
	}
	
	@Override
	public CustomerDto queryph(String customer_name) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_customer where customer_name=?";
		CustomerDto user =null;
		try {
			user = jdbcTemplate.queryForObject(sql, new Object[]{customer_name},new BeanPropertyRowMapper<CustomerDto>(CustomerDto.class));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;
	}
	
	@Override
	public int register(String customer_name, String customer_password) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_customer(customer_name,customer_password,customer_status) values(?,?,?)";
		int yan=jdbcTemplate.update(sql, new Object[]{customer_name,customer_password,"0"});
		return yan;
	}

	@Override
	public int reset(String customer_name, String customer_password,long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_customer set customer_password=? where customer_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{customer_password,customer_id});
		return yan;
	}

	@Override
	public int zeng(long customer_id) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_material(customer_id,rz_status) values(?,?)";
		int yan=jdbcTemplate.update(sql, new Object[]{customer_id,"0"});
		return yan;
	}

	@Override
	public int updatemima(String password1, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_customer set customer_password=? where customer_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{password1,customer_id});
		return yan;
	}

	@Override
	public int yanmima(String customer_password, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_customer where customer_id=? and customer_password=?";
		CustomerDto user = null;
		try {
			user = jdbcTemplate.queryForObject(sql, new Object[]{customer_id,customer_password},new BeanPropertyRowMapper<CustomerDto>(CustomerDto.class));
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
		if(user!=null){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public int loginphyz(String customer_name) {
		// TODO Auto-generated method stub
		String sql = "select count(*) from tb_customer where customer_name=?";
		int user =-1;
		try {
			user = jdbcTemplate.queryForInt(sql, new Object[]{customer_name});
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
		return user;
	}

	@Override
	public List<Map<String, Object>> queryUser() {
		// TODO Auto-generated method stub
		String sql = "select * from tb_user";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public UserDto queryUser(long user_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_user where user_id=?";
		UserDto user = null;
		try {
			user = jdbcTemplate.queryForObject(sql, new Object[]{user_id},new BeanPropertyRowMapper<UserDto>(UserDto.class));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;
	}

	@Override
	public int add(UserDto vo) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_user(user_name,user_password,user_phone,true_name,user_status,user_role) values(?,?,?,?,?,?)";
		int yan=jdbcTemplate.update(sql, new Object[]{vo.getUser_name(),vo.getUser_password(),vo.getUser_phone(),
										vo.getTrue_name(),"1",vo.getUser_role()});
		return yan;
	}

	@Override
	public int updateUser(UserDto vo) {
		// TODO Auto-generated method stub
		String sql = "update tb_user set user_name=?,user_password=?,user_phone=?,true_name=?,user_status=?,user_role=? where user_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{vo.getUser_name(),vo.getUser_password(),vo.getUser_phone(),
				vo.getTrue_name(),vo.getUser_status(),vo.getUser_role(),vo.getUser_id()});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryCost() {
		// TODO Auto-generated method stub
		String sql = "select * from tb_cost_method";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public CostMethodDto updateCost(long cost_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_cost_method where cost_id=?";
		CostMethodDto user = null;
		try {
			user = jdbcTemplate.queryForObject(sql, new Object[]{cost_id},new BeanPropertyRowMapper<CostMethodDto>(CostMethodDto.class));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;
	}

	@Override
	public int updateCostDo(CostMethodDto vo) {
		// TODO Auto-generated method stub
		String sql = "update tb_cost_method set cost_month=?,cost_day_rate=?,cost_status=? where cost_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{vo.getCost_month(),vo.getCost_day_rate(),
				vo.getCost_status(),vo.getCost_id()});
		return yan;
	}

	@Override
	public int addCost(CostMethodDto vo) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_cost_method(cost_month,cost_day_rate,cost_status) values(?,?,?)";
		int yan=jdbcTemplate.update(sql, new Object[]{vo.getCost_month(),vo.getCost_day_rate(),vo.getCost_status()});
		return yan;
	}
}
